﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class user_grant_group : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string userid = this.Request["userid"].ToString();
            TUser1 oUser = new TUser1(userid);
            oUser.Select();
            user_name.Text = oUser.UserName;
            user_id.Text = userid;
            //----
            switchmode.Text = "<a href='grant_single.aspx?userid=" + userid + "'>单授权</a> <-> <b>组授权</b>";
            //----
            DisplayRS1();
            DisplayRS2();
        }
    }

    private void DisplayRS1()
    {
        lbLeft.Items.Clear();
        //string sSql = "select grpcode,grpname from t_user_grantgroup where status=0 and grpcode not in (select gfcode from t_user_access where userid='" + user_id.Text + "') order by sortnum";
        string sSql = "select grpcode,grpname from t_user_grantgroup where status=0 order by sortnum";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            ListItem Item = new ListItem(myReader.FDataReader.GetValue(1).ToString(), myReader.FDataReader.GetValue(0).ToString());
            lbLeft.Items.Add(Item);
        }
        myReader.Free();
    }

    private void DisplayRS2()
    {
        lbRight.Items.Clear();
        string sSql = "select grpcode,grpname from t_user_grantgroup where status=0 and grpcode in (select gfcode from t_user_access where userid='" + user_id.Text + "') order by sortnum";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            ListItem Item = new ListItem(myReader.FDataReader.GetValue(1).ToString(), myReader.FDataReader.GetValue(0).ToString());
            lbRight.Items.Add(Item);
        }
        myReader.Free();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TUserAccess oItem = new TUserAccess();
        oItem.UserID = user_id.Text;
        oItem.GFCode = lbLeft.SelectedValue;
        oItem.Insert();
        //----
        DisplayRS1();
        DisplayRS2();
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        TUserAccess.DeleteByItem(user_id.Text, lbRight.SelectedValue);
        //----
        DisplayRS1();
        DisplayRS2();
    }
}
